package com.mtzqc.common.ui.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder

abstract class BaseRecyclerActivity<VB : ViewBinding, AD : BaseQuickAdapter<Any, out BaseViewHolder>>(
    inflate: (LayoutInflater) -> VB
) :
    TitleBarBindingActivity<VB>(inflate) {
    /**
     * 第一页编号
     */
    open fun injectFirstPageNum(): Int {
        return 1
    }

    /**
     * 请求第一页的page
     */
    open fun injectPageSize(): Int {
        return 20
    }


    open fun initItemDecoration(rv: RecyclerView) {
        rv.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL))
    }

    open fun initLayoutManager(rv: RecyclerView) {
        rv.layoutManager = LinearLayoutManager(this)
    }

    open fun initItemAnimator(rv: RecyclerView) {

    }

    lateinit var mRecyclerView: RecyclerView
    lateinit var mAdapter: AD
    var mRefreshLayout: SwipeRefreshLayout? = null

    abstract fun findRecyclerView(): RecyclerView

    open fun initRecyclerView(rv: RecyclerView) {
        initItemAnimator(rv)
        initItemDecoration(rv)
        initLayoutManager(rv)
    }


    abstract fun findAdapter(): AD

    open fun <T> initAdapter(rv: RecyclerView): AD {
        mAdapter = findAdapter()
        injectHeaderView(rv, mAdapter)
        injectEmptyView(rv, mAdapter)
        injectFooterView(rv, mAdapter)
        injectLoadMore(rv, mAdapter)
        return mAdapter
    }


    override fun initView(savedInstanceState: Bundle?) {
        findRecyclerView().let {
            mRecyclerView = it
            initRecyclerView(it)
            it.adapter = initAdapter<Any>(it)
        }
        injectRefreshLayout()
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        loadFirst()
    }

    private fun loadFirst() {
        showLoading()
        loadData(true, injectFirstPageNum(), injectPageSize())
    }

    abstract fun loadData(first: Boolean, page: Int, pageSize: Int)


    override fun onRetryBtnClick() {
        super.onRetryBtnClick()
        loadFirst()
    }


    open fun enableRefresh(): Boolean {
        return true
    }

    open fun findRefreshLayout(): SwipeRefreshLayout? {
        return null
    }

    open fun injectRefreshLayout() {
        if (enableRefresh()) {
            mRefreshLayout = findRefreshLayout()
            mRefreshLayout?.setOnRefreshListener {
                onSwipeRefresh()
            }
        }
    }

    open fun onSwipeRefresh() {
        loadData(false, injectFirstPageNum(), injectPageSize())
    }


    open fun enableHeaderView(): Boolean {
        return false
    }

    open fun headerLayout(): Int {
        return 0
    }

    open fun createHeaderView(rv: RecyclerView): View {
        var layout = headerLayout()
        if (layout != 0) {
            return LayoutInflater.from(this).inflate(layout, rv, false)
        }
        throw RuntimeException("header layout is empty")
    }

    open fun injectHeaderView(
        rv: RecyclerView,
        adapter: BaseQuickAdapter<*, *>
    ) {
        if (enableHeaderView()) {
            createHeaderView(rv).apply {
                initHeaderViewData(this)
                adapter.addHeaderView(this)
            }
        }
    }

    open fun initHeaderViewData(headerView: View) {

    }

    open fun enableEmptyView(): Boolean {
        return true
    }

    abstract fun emptyLayout(): Int

    open fun createEmptyView(rv: RecyclerView): View {
        var layout = emptyLayout()
        if (layout != 0) {
            return LayoutInflater.from(this).inflate(layout, rv, false)
        }
        throw RuntimeException("empty layout is empty")
    }

    open fun injectEmptyView(
        rv: RecyclerView,
        adapter: BaseQuickAdapter<*, *>
    ) {
        if (enableEmptyView()) {
            createEmptyView(rv).apply {
                initEmptyViewData(this)
                adapter.setEmptyView(this)
            }
        }
    }

    open fun initEmptyViewData(headerView: View) {

    }

    open fun enableFooterView(): Boolean {
        return false
    }


    open fun footerLayout(): Int {
        return 0
    }

    open fun createFooterView(rv: RecyclerView): View {
        var layout = footerLayout()
        if (layout != 0) {
            return LayoutInflater.from(this).inflate(layout, rv, false)
        }
        throw RuntimeException("header layout is empty")
    }

    open fun injectFooterView(
        rv: RecyclerView,
        adapter: BaseQuickAdapter<*, *>
    ) {
        if (enableFooterView()) {
            createFooterView(rv).apply {
                initFooterViewData(this)
                adapter.addFooterView(this)
            }
        }
    }

    open fun initFooterViewData(headerView: View) {

    }


    open fun enableLoadMore(): Boolean {
        return false
    }


    open fun injectLoadMore(
        rv: RecyclerView,
        adapter: BaseQuickAdapter<*, *>
    ) {
        if (enableLoadMore() && adapter is LoadMoreModule) {
            adapter.loadMoreModule.apply {
                isEnableLoadMore = true
                preLoadNumber = preLoadNumber()
                setOnLoadMoreListener {
                    onAdapterMore()
                }
            }
        }
    }

    open fun onAdapterMore() {

    }

    open fun preLoadNumber(): Int {
        return 3
    }

    open fun finishRefresh(success: Boolean = true) {
        mRefreshLayout?.isRefreshing = false
        if (loadSirEnable()) {
            if (success) {
                showPageSuccess()
            } else {
                showPageError()
            }
        }
    }

    open fun finishLoadMore(array: List<*>?, gone: Boolean = false) {
        if (enableLoadMore() && mAdapter is LoadMoreModule) {
            mAdapter.loadMoreModule.apply {
                if (null == array) {
                    loadMoreFail()
                } else if (array.size == injectPageSize()) {
                    loadMoreComplete()
                } else {
                    loadMoreEnd(gone)
                }
            }
        }

    }

    override fun loadSirTarget(): Any {
        return (if (null == mRefreshLayout) mRecyclerView else mRefreshLayout) as Any
    }
}